package io.gitee.falllee.dao.config;

import org.springframework.core.NamedThreadLocal;

/**
 * 保存多线程的DAO数据源
 * 
 * @author batty
 * @date 2019/05/16
 */
public final class DaoSourceContextHolder {

    private DaoSourceContextHolder() {}

    private static final ThreadLocal<String> DAOSOURCE_HOLDER = new NamedThreadLocal<>("DaoSource");
    
    private static final ThreadLocal<Integer> CACHE_HOLDER = new NamedThreadLocal<>("CacheCount");
    
    private static final ThreadLocal<Integer> DB_HOLDER = new NamedThreadLocal<>("DbCount");

    public static String getDaoSource() {
        return DAOSOURCE_HOLDER.get();
    }

    public static void setDaoSource(String daoSource){
        DAOSOURCE_HOLDER.set(daoSource);
    }
    
    public static Integer getCacheCount() {
        return CACHE_HOLDER.get() != null ? CACHE_HOLDER.get() : 0;
    }
    
    public static void setCacheCount(Integer cacheCount) {
        CACHE_HOLDER.set(cacheCount);
    }
    
    public static Integer getDbCount() {
        return DB_HOLDER.get() != null ? DB_HOLDER.get() : 0;
    }
    
    public static void setDbCount(Integer dbCount) {
        DB_HOLDER.set(dbCount);
    }

    public static void clear() {
        DAOSOURCE_HOLDER.remove();
        CACHE_HOLDER.remove();
        DB_HOLDER.remove();
    }
}
